package fr.imag.ufrima.ima.project.ui.tools;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.apache.log4j.Logger;

import fr.imag.ufrima.ima.project.core.Image;
import fr.imag.ufrima.ima.project.core.operations.Sharpen;
import fr.imag.ufrima.ima.project.ui.main.ImageProject;

public class SharpenTool extends JPanel {
    private Logger logger = Logger.getLogger(getClass().getName());
	
	private ImageProject imageProject;
	private JLabel lblValue;

	private JSlider sliderSharpen;
	
	/**
	 * Create the panel.
	 */
	public SharpenTool(ImageProject imageProject) {
		this.imageProject = imageProject;
		
		setLayout(new BorderLayout(0, 0));
		
		JPanel panel = new JPanel();
		add(panel, BorderLayout.NORTH);
		GridBagLayout gbl_panel = new GridBagLayout();
		gbl_panel.columnWidths = new int[]{180};
		gbl_panel.rowHeights = new int[]{0, 0, 0,0,0,0};
		gbl_panel.columnWeights = new double[]{0.0};
		gbl_panel.rowWeights = new double[]{0.0, 0.0, Double.MIN_VALUE};
		panel.setLayout(gbl_panel);
		
		JLabel lblMessage = new JLabel("Intensité:");
		GridBagConstraints gbc_lblMessage = new GridBagConstraints();
		gbc_lblMessage.anchor = GridBagConstraints.WEST;
		gbc_lblMessage.insets = new Insets(10, 0, 5, 0);
		gbc_lblMessage.gridx = 0;
		gbc_lblMessage.gridy = 0;
		panel.add(lblMessage, gbc_lblMessage);
		
		sliderSharpen = new JSlider(1, 10, 1);
		GridBagConstraints gbc_btnFlou;
		GridBagConstraints gbc_sliderSharpen = new GridBagConstraints();
		gbc_sliderSharpen.fill = GridBagConstraints.BOTH;
		gbc_sliderSharpen.insets = new Insets(0, 0, 5, 0);
		gbc_sliderSharpen.gridx = 0;
		gbc_sliderSharpen.gridy = 1;
		sliderSharpen.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				lblValue.setText(Integer.toString(sliderSharpen.getValue()));
			}
		});
		panel.add(sliderSharpen, gbc_sliderSharpen);
		
		lblValue = new JLabel("1");
		gbc_btnFlou = new GridBagConstraints();
		gbc_btnFlou.insets = new Insets(0, 0, 5, 0);
		gbc_btnFlou.gridx = 0;
		gbc_btnFlou.gridy = 3;
		panel.add(lblValue, gbc_btnFlou);
		
		JButton btnApply = new JButton("Appliquer");
		GridBagConstraints gbc_btnApply = new GridBagConstraints();
		gbc_btnApply.fill = GridBagConstraints.BOTH;
		gbc_btnApply.insets = new Insets(0, 0, 5, 0);
		gbc_btnApply.gridx = 0;
		gbc_btnApply.gridy = 4;
		btnApply.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				SharpenTool.this.applySharpening();
			}
		});
		panel.add(btnApply, gbc_btnApply);
	}
	
	private void applySharpening() {
				
		Sharpen sharpen = new Sharpen(imageProject.getCurrentImageDocument().getImage());
		sharpen.setRepetitions(sliderSharpen.getValue());
		
		imageProject.applyOperation(sharpen);
	}
}
